package com.yh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yh.entity.Books;
import com.yh.entity.Page;
import com.yh.services.IBooksService;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
@Controller
public class BooksAction extends ActionSupport implements ModelDriven {
    private Books books;
    private Page page;
    private int pageSize;
    private int pageNum;
    @Autowired
    private IBooksService iBooksService;


    public String show() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("book", books);
        page = iBooksService.getLikeAllByX(map);
        return SUCCESS;
    }

    public String addBooks() {
        iBooksService.addBooks(books);
        return SUCCESS;
    }

    public String delBooks() {
        iBooksService.delBooks(books);
        return SUCCESS;
    }

    public String updateBooks() {
        iBooksService.updateBooks(books);
        return SUCCESS;
    }

    public String getBooksById() {
      books= iBooksService.getBooksById(books);
        ServletActionContext.getRequest().setAttribute("book",books);
        return "perfect";
    }


    @Override
    public Object getModel() {
        if (books == null) {
            books = new Books();
        }
        return books;
    }
}
